package com.hsy.linemoney.modules.home.transfer;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ListView;

import com.deparse.dpframe.tools.utils.DPLogUtil;
import com.hsy.linemoney.R;
import com.hsy.linemoney.modules.common.CommonBaseActivityWithSimpleActionBar;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by Administrator on 2016/6/30.
 */
public class TransferAccountActivity extends CommonBaseActivityWithSimpleActionBar {
    private ListView list;
    private List<HashMap<String, String>> rateListData;
    private Adapter adapter;
    private LayoutInflater inflater;
    private List<Boolean> checkBox = new ArrayList<>();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_transfer_account);
        setTitle("收款");
        list = F(R.id.list);
        inflater = LayoutInflater.from(this);

        rateListData = new ArrayList<>();

        for (int i = 0; i < 26; i++) {
            HashMap<String, String> map = new HashMap<>();
            map.put("name", "5name" + i);
            rateListData.add(map);
        }

        for (int i = 0; i < rateListData.size(); i++) {
            checkBox.add(false);
        }

        adapter = new Adapter();
        list.setAdapter(adapter);


        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (checkBox.get(position) == true) {
                    checkBox.set(position, false);
                } else {
                    for (int i = 0; i < rateListData.size(); i++) {
                        if (i == position) {
                            checkBox.set(position, true);
                        } else {
                            checkBox.set(i, false);
                        }

                    }


                    DPLogUtil.d("checkBox==" + checkBox.get(position));
                }
                DPLogUtil.d("checkBox==" + checkBox.get(position));
                CheckBox checkBox = (CheckBox) list.getChildAt(position).findViewById(R.id.checkbox);
                adapter.updateView(checkBox, position);
                adapter.notifyDataSetChanged();
            }
        });
    }

    private class Adapter extends BaseAdapter {


        @Override
        public int getCount() {
            return rateListData.size();
        }

        @Override
        public Object getItem(int position) {
            return rateListData.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            Holder holder = null;
            if (null == convertView) {
                holder = new Holder();
                convertView = inflater.inflate(R.layout.item_transfer_list, null);
                holder.checkbox = (CheckBox) convertView.findViewById(R.id.checkbox);
                convertView.setTag(holder);
            } else {
                holder = (Holder) convertView.getTag();
            }

            setData(holder.checkbox, position);
            return convertView;
        }

        private void setData(CheckBox checkbox, int position) {
            if (checkBox.get(position) == true) {
                checkbox.setChecked(true);
            } else {
                checkbox.setChecked(false);
            }
        }

        public void updateView(CheckBox checkbox, int position) {
            setData(checkbox, position);

        }

        private class Holder {
            private CheckBox checkbox;

        }

    }
}
